package com.joplus.housekeeper.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import com.hyphenate.easeui.ui.EaseBaseActivity;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.XmppUser;

import com.joplus.housekeeper.ui.fragment.ConvertisementFragment;
import com.joplus.housekeeper.utils.RealmHelper;
import com.joplus.housekeeper.utils.T;
import com.joplus.housekeeper.xmpp.SmackManager;

import de.hdodenhof.circleimageview.CircleImageView;
import io.realm.RealmResults;


public class MainActivity extends EaseBaseActivity {


    private TextView tv_title;
    private CircleImageView iv_service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_service = (CircleImageView) findViewById(R.id.iv_service);
        RealmResults<XmppUser> xmppUsers = RealmHelper.instance().allUser();
        tv_title.setText("会话(" + xmppUsers.size() + ")");

        ConvertisementFragment convertisementFragment = new ConvertisementFragment();

        getSupportFragmentManager().beginTransaction().add(R.id.fl_container, convertisementFragment)
                .show(convertisementFragment)
                .commit();


        SmackManager.getInstance().addMessageListener();
  //      SmackManager.getInstance().addPacketeListener();
        // SmackManager.getInstance().addFileListener();
        //  SmackManager.getInstance().offlineMessage( SmackManager.getInstance().getConnection());
    }


    int keyDownCount = 0;

    @Override
    public void onBackPressed() {
        keyDownCount++;

        if (keyDownCount == 1) {
            T.showShort(MainActivity.this, "再按一次退出JO+管家");
        }
        if (keyDownCount >= 2) {

            SmackManager.getInstance().logout();
            SmackManager.getInstance().disconnect();
            finish();
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                keyDownCount = 0;
            }
        }, 3500);


    }
}
